package com.sgb.zhaocai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import com.google.android.material.tabs.TabLayout;
import com.sgb.link.view.adapter.PickNoticeAdapter;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityPickInvitedApplyListBinding;
import com.sgb.zhaocai.view.fragment.PickInvitedApplyFragment;
import com.sgb.zhaocai.view.fragment.PickInvitedListFragment;
import com.sgb.zhaocai.view.fragment.WebSiteInvitedListFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.MToast;
import java.util.ArrayList;


/**
 * 邀请报名列表页    招标，竞价
 **/

public class PickInvitedApplyListActivity extends AppActivity {

    private ActivityPickInvitedApplyListBinding binding;
    private ArrayList<String> titles = new ArrayList<>();
    private ArrayList<Fragment> mFragments;
    private boolean isSearching = false;
    private String searchName = "";
    private String fromWhere;   //1.招标 2.竞价
    private PickInvitedApplyFragment pickInvitedApplyFragment;
    private PickInvitedListFragment pickInvitedListFragment;
    private WebSiteInvitedListFragment websiteinvitedlistfragment;

    public static void start(Context context, String fromWhere) {
        Intent intent = new Intent(context, PickInvitedApplyListActivity.class);
        intent.putExtra("fromWhere", fromWhere);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        fromWhere = getIntent().getStringExtra("fromWhere");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_invited_apply_list);
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        super.init();
        binding.includeSearch.etView.setHint("请输入招标名称");
        if ("1".equals(fromWhere)) {
            titles.add("邀请报名");
            binding.includeTitle.titleView.setText("邀请报名");
            binding.includeSearch.etView.setHint("请输入招标名称");
        } else if ("2".equals(fromWhere)) {
            titles.add("邀请竞价");
            binding.includeTitle.titleView.setText("邀请竞价");
            binding.includeSearch.etView.setHint("请输入竞价名称");
        }
        titles.add("邀请记录");
        titles.add("非网站客户邀请记录");
        mFragments = new ArrayList<>();
        mFragments.add(PickInvitedApplyFragment.newInstance(fromWhere));
        mFragments.add(PickInvitedListFragment.newInstance(fromWhere));
        mFragments.add(WebSiteInvitedListFragment.newInstance(fromWhere));

        pickInvitedApplyFragment = (PickInvitedApplyFragment) mFragments.get(0);
        pickInvitedListFragment = (PickInvitedListFragment) mFragments.get(1);
        websiteinvitedlistfragment = (WebSiteInvitedListFragment) mFragments.get(2);

        PickNoticeAdapter pagerAdapter = new PickNoticeAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(mFragments.size());
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabData(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.includeSearch.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.includeSearch.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (isSearching) return true;
                if (TextUtils.isEmpty(binding.includeSearch.etView.getText().toString())) {
                    MToast.showToast(PickInvitedApplyListActivity.this, binding.includeSearch.etView.getHint().toString());
                    return true;
                }
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    searchUpdate();
                }
                return false;
            }
        });

        binding.includeSearch.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    binding.includeSearch.tvSearchDelete.setVisibility(View.GONE);
                } else {
                    binding.includeSearch.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        binding.includeTitle.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.includeSearch.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.includeSearch.etView.setText("");
                searchName = "";
                if (binding.viewpager.getCurrentItem() == 0) {
                    pickInvitedApplyFragment.updateData(searchName, fromWhere);
                } else if (binding.viewpager.getCurrentItem() == 1) {
                    pickInvitedListFragment.updateData(searchName, fromWhere);
                }else if (binding.viewpager.getCurrentItem() ==2){
                    websiteinvitedlistfragment.updateData(searchName);
                }
            }
        });

        binding.includeSearch.searchText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(binding.includeSearch.etView.getText().toString())) {
                    MToast.showToast(PickInvitedApplyListActivity.this, binding.includeSearch.etView.getHint().toString());
                    return;
                }
                searchUpdate();
            }
        });
    }

    private void changeTabData(int position) {
        switch (position) {
            case 0:
                if ("1".equals(fromWhere)) {
                    binding.includeSearch.etView.setHint("请输入招标名称");
                } else if ("2".equals(fromWhere)) {
                    binding.includeSearch.etView.setHint("请输入竞价名称");
                }
                break;
            case 1:
                binding.includeSearch.etView.setHint("请输入客户名称");
                break;
            case 2:
                binding.includeSearch.etView.setHint("请输入招标名称、企业名称、联系人");
                break;
        }
    }

    private void searchUpdate(){
        isSearching = true;
        searchName = binding.includeSearch.etView.getText().toString();
        if (!TextUtils.isEmpty(searchName)) {
            isSearching = false;
            if (binding.viewpager.getCurrentItem() == 0) {
                pickInvitedApplyFragment.updateData(searchName, fromWhere);
            } else if (binding.viewpager.getCurrentItem() == 1) {
                pickInvitedListFragment.updateData(searchName, fromWhere);
            }else if (binding.viewpager.getCurrentItem() == 2){
               websiteinvitedlistfragment.updateData(searchName);
            }
        }
    }
}